<?php
/**
 * 文章分类服务
 * User: t.young
 * Date: 2018/5/17
 * Time: 11:25
 */

namespace backend\service;


use backend\component\BackendBaseService;
use backend\component\ResultEasyUIFormat;
use backend\component\ResultFormat;
use common\models\info\Category;

class CategoryService extends BackendBaseService
{
    /**
     * 获取分类列表信息
     * @param $post
     * @return ResultEasyUIFormat
     */
    public static function categoryList($post) {
        $model = new Category();
        $table = $model->dataListTable($post);
        return ResultEasyUIFormat::tableFormat($table)->asJson();
    }
    public static function deleteCategory($id){
        $model = Category::findOne($id);
        if (!$model || $model["id"] != $id) {
            return ResultFormat::error("这条数据不存在!");
        }
        try{
            if (!$model->delete()) {
                return ResultFormat::error($model->getErrorsToString());
            }
            return ResultFormat::success("删除成功");
        }catch (\Exception $e) {
            return ResultFormat::error($e->getMessage());
        }
    }
    /**
     * 清空回收站
     * @param $id
     * @return string
     */
    public static function clearRecycle($id) {
        $model = new Category();
        $result = $model->clearRecycle($id);
        if ($result === false) {
            return ResultFormat::error($model->getErrorsToString());
        }
        return ResultFormat::success("操作成功");
    }

    /**
     * 还原分类
     * @param $id
     * @return string
     */
    public static function undoCategory($id) {
        $model = new Category();
        $result = $model->undoCategory($id);
        if ($result == false) {
            return ResultFormat::error($model->getErrorsToString());
        }
        return ResultFormat::success("操作成功");
    }

    /**
     * 获取分类数据列表按指定的id
     * @param $cat_id
     * @return  string [{"id":0,"name":"fdsa"}]
     */
    public static function categoryForComboboxData($cat_id,$is_first) {
        $result = Category::categoryForComboboxData($cat_id,$is_first);
        return json_encode($result);
    }

}